package DB;

import Model.*;

import java.sql.*;
import java.util.ArrayList;

public class DBUdlejningstype
{
	private Connection con;

	private static DBUdlejningstype instance = null;
    
    public static DBUdlejningstype getInstance()
    {
        if (instance == null)
        {
          instance = new DBUdlejningstype();
        }
        return instance;
    }
	
	public DBUdlejningstype()
	{
		con = DBConnection.getInstance().getDBcon();
	}
	
	private void create(String wClause)
	{
	    String query="INSERT INTO Udlejningstype";

		if (wClause.length()>0)
		{
			query=query+ wClause;
			
			Statement stmt;
			
			try
			{
				stmt = con.createStatement(); 
				stmt.executeUpdate(query); 
				con.commit(); 
				stmt.close();
			}
			catch (Exception e)
			{ 
				e.printStackTrace(); 
			}
		}
		else
		{
		}
	}
	
	public void createUdlejningstype(Udlejningstype u)
	{
		String wClause = "VALUES " 
	        + "('" + u.getTypenavn() + "',"
	        + "" + u.getPris() + ")";
	        create(wClause);
	}
	
	public void updateUdlejningstype(Udlejningstype u)
	{
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		   ("UPDATE UdlejningstypeSET " 
				+ "Typenavn = '" + u.getTypenavn() + "',"
		   		+ "Pris = " + u.getPris() + ","
		   	+ " WHERE Udlejningsid = " + u.getUdlejningstypeid() + "");
		  prepstmt.executeUpdate();
		             
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	}
	
	public void deleteUdlejningstype(int uId)
	{
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		    ("DELETE FROM Udlejningstype WHERE udlejningstypeid = " + uId + "");   
		  prepstmt.executeUpdate();          
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Udlejningstype";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	private Udlejningstype singleWhere(String wClause)
	{
		ResultSet results;
		Udlejningstype u = new Udlejningstype();
	    String query =  buildQuery(wClause);
		try{ // read the customer from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            u = buildUdlejningstype(results);
                            //association is to be build
                            stmt.close();
			}
		
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return u;
	}
	
	public Udlejningstype findUdlejningstypeNavn(String navn) 
    {
    	String wClause = " typenavn = '" + (navn) + "' ";
    	return singleWhere(wClause);
    }
	
	public Udlejningstype findUdlejningstypeId(int id)
	{
		String wClause = " udlejningstypeid = " + id + "";
		return singleWhere(wClause);
	}
	
	public ArrayList<Udlejningstype> getAlleUdlejningstype()
	{
		return miscWhere("");
	}
	
	private ArrayList<Udlejningstype> miscWhere(String wClause)
	{
		ResultSet results;
		ArrayList list = new ArrayList();	
		
		String query =  buildQuery(wClause);
  		try
  		{ // read the customer from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);		 		

			while( results.next() )
			{
		     	 Udlejningstype udlObj = new Udlejningstype();
		     	 udlObj = buildUdlejningstype(results);	
                 list.add(udlObj);	
			}//end while
                        // if retrieveAssociation is true, the ZipCity has to be selected for each object in the list
			stmt.close();

		}//slut try	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	private Udlejningstype buildUdlejningstype(ResultSet results)
    {   
		DBFormat dbForm = DBFormat.getInstance();
		Udlejningstype udl = new Udlejningstype();
          try
          {
        	    udl.setUdlejningstypeid(results.getInt(1));
        	    udl.setTypenavn(results.getString(2));
        	    udl.setPris(results.getDouble(3));
          }
         catch(Exception e)
         {
             System.out.println("error in building the udlejningstype object");
         }
         return udl;
    }

	public ArrayList<Udlejningstype> findUdlejningstypePerson(int personid) 
	{
		String query="SELECT * FROM Udlejningstype where " + personid + "= kundeid";
		return null;
	}
	
}
